#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 3e5 + 5;

int n, m;
int a[N];
int f[N], g[N];
int l[N], r[N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> a[i];
  rep(i, 1, n) {
    l[i] = max(f[a[i]], f[a[i] - 1]) + 1;
    f[a[i]] = i;
  }
  rep(i, 0, n) g[i] = n + 1;
  per(i, n, 1) {
    r[i] = g[a[i] - 1] - 1;
    g[a[i]] = i;
  }
  ll ans = 0;
  rep(i, 1, n) ans += 1ll * (i + 1 - l[i]) * (r[i] + 1 - i);
  cout << ans;
  return 0;
}